package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.360che.com/ 卡车之家
 *  abc
 */
public class _360cheNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://www.360che.com/.*/[0-9]{6}/[0-9]*.html"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String source="卡车之家";

		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select(".article h1").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("#ArticleContent"));
		if(title==null || title.trim().length()==0){
		}
		date=DateUtils.matchDate(doc.select(".article_info").text());
		String author=null;
		author=JSoupUtils.matchAuthor(doc,"来源："," 作者：");
		news.setAuthor(author);

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		/**
		 * 解析用于更新的地址
		 */
		String updateUrl=null;

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		_360cheNewsAnalyse a=new _360cheNewsAnalyse();
		String url="http://www.360che.com/tech/150625/41971.html#pvareaid=1010502";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println("url="+fromatUrl);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate() {
		return false;
	}
}
